package com.android.hedgehog.puzzles;

import java.util.List;
import java.util.Random;

import android.graphics.Color;

import com.android.hedgehog.Assets;
import com.android.hedgehog.framework.Game;
import com.android.hedgehog.framework.Graphics;
import com.android.hedgehog.framework.Input.TouchEvent;
import com.android.hedgehog.puzzles.MathScreen.GameState;

public class TimeBombScreen extends PuzzleScreen {
	enum GameState {
		Ready, Run, Finish
	}

	enum AlphaStage {
		turnOn, On, turnOff, Off
	}

	Random rand = new Random();
	Graphics graphics;
	int alpha;
	int alphaDeath;
	double alphaTime;
	AlphaStage stage;
	GameState state;
	double totalTime;
	double resultTime;
	int resultTimeLimit = 4;
	TimeBombField bomb;
	int difficulty;

	public TimeBombScreen(Game game, int gameType, int difficulty, int instNumber) {
		this.gameType = gameType;
		graphics = game.getGraphics();
		bomb = new TimeBombField(difficulty);
		this.difficulty = difficulty;
		alpha = 0;
		stage = AlphaStage.turnOn;

		if (instNumber == 1)
			state = GameState.Ready;
		else
			state = GameState.Run;
	}

	@Override
	public void update(List<TouchEvent> touchEvents, double deltaTime) {

		totalTime += deltaTime;

		if (state == GameState.Ready)
			updateReady();
		if (state == GameState.Run)
			updateRun(touchEvents, deltaTime);
		if (state == GameState.Finish)
			updateFinish(deltaTime);

	}

	private void updateReady() {
		if (totalTime > 2)
			state = GameState.Run;
	}

	private void updateRun(List<TouchEvent> touchEvents, double deltaTime) {

		visibility(deltaTime);

		int len = touchEvents.size();
		for (int i = 0; i < len; i++) {// FOR
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_DRAGGED) {
				if (bomb.cutWire(event.x, event.y)) {
					state = GameState.Finish;
				}
				if (bomb.nextWireToCut == bomb.numberOfWires) {
					success = true;
					instanceScore = 0 + difficulty;
					resultTimeLimit = 2;
					state = GameState.Finish;
				}
			}
		}// FOR

		if (totalTime >= bomb.bombTime)
			state = GameState.Finish;

	}

	private void updateFinish(double deltaTime) {
		resultTime += deltaTime;
		if (resultTime > resultTimeLimit)
			instanceOver = true;
		if (alphaDeath < 255)
			alphaDeath += 5;

	}

	private void visibility(double deltaTime) {

		if (stage == AlphaStage.turnOn) {
			alpha += 5;
			if (alpha == 255)
				stage = AlphaStage.On;
		}

		if (stage == AlphaStage.On) {
			alphaTime += deltaTime;
			if (alphaTime >= bomb.onTime) {
				stage = AlphaStage.turnOff;
				alphaTime = 0.0;
			}
		}

		if (stage == AlphaStage.turnOff) {
			alpha -= 5;
			if (alpha == 0)
				stage = AlphaStage.Off;
		}

		if (stage == AlphaStage.Off) {
			alphaTime += deltaTime;
			if (alphaTime >= bomb.offTime) {
				stage = AlphaStage.turnOn;
				alphaTime = 0.0;
			}
		}

	}

	@Override
	public void present() {
		graphics.drawRect3(0, 31, 320, 450, 255, 192, 192, 192);

		graphics.drawLine(0, 100, 320, 100, Color.RED);

		for (int i = 0; i < bomb.numberOfWires; i++) { // WIRES
			if (bomb.wires[i].cut == false) {
				if (i < 6) {
					graphics.drawPixmap(Assets.wires2, bomb.wires[i].x, bomb.wires[i].y, bomb.wires[i].srcX,
							bomb.wires[i].srcY, bomb.wires[i].width, bomb.wires[i].height);
				} else {
					graphics.drawPixmap(Assets.wires, bomb.wires[i].x, bomb.wires[i].y, bomb.wires[i].srcX,
							bomb.wires[i].srcY, bomb.wires[i].width, bomb.wires[i].height);
				}
			}
		} // WIRES

		graphics.drawPixmap(Assets.timebomb, 70, 170); // BOMB

		if (state == GameState.Ready)
			presentReady();
		if (state == GameState.Run)
			presentRun();
		if (state == GameState.Finish)
			presentFinish();

	}

	private void presentReady() {
		graphics.drawUniversalText("Cut Wires In Order", 2, true, 160, 70, 28, 255, Color.BLACK);

	}

	private void presentRun() {

		for (int i = 0; i < 14; i++) { // TEXT
			if (bomb.nextWireToCut <= i) {
				if (i < 10) {
					if (i == bomb.mysteryPosition)
						graphics.drawHiddenText(i + 1 + ". ?????", 32 + ((i % 5) * 64), 50 + ((i / 5) * 20),
								alpha, 13, Color.BLACK);
					else
						graphics.drawHiddenText(i + 1 + "." + bomb.colors[bomb.wireOrder[i]],
								32 + ((i % 5) * 64), 50 + ((i / 5) * 20), alpha, 13, Color.BLACK);
				} else {
					if (i == bomb.mysteryPosition)
						graphics.drawHiddenText(i + 1 + ". ?????", 40 + ((i % 5) * 80), 50 + ((i / 5) * 20),
								alpha, 13, Color.BLACK);
					else
						graphics.drawHiddenText(i + 1 + "." + bomb.colors[bomb.wireOrder[i]],
								40 + ((i % 5) * 80), 50 + ((i / 5) * 20), alpha, 13, Color.BLACK);
				}

			} else {
				if (i < 10) {
					graphics.drawHiddenText(i + 1 + "." + bomb.colors[bomb.wireOrder[i]],
							32 + ((i % 5) * 64), 50 + ((i / 5) * 20), 255, 13, Color.BLACK);
					graphics.drawLine(6 + ((i % 5) * 64), 44 + ((i / 5) * 20), 58 + ((i % 5) * 64),
							44 + ((i / 5) * 20), Color.BLACK);
				} else {
					graphics.drawHiddenText(i + 1 + "." + bomb.colors[bomb.wireOrder[i]],
							40 + ((i % 5) * 80), 50 + ((i / 5) * 20), 255, 13, Color.BLACK);
					graphics.drawLine(14 + ((i % 5) * 80), 44 + ((i / 5) * 20), 66 + ((i % 5) * 80),
							44 + ((i / 5) * 20), Color.BLACK);
				}
			}
		} // TEXT

		graphics.drawSpecialText("0:" + (bomb.bombTime - (int) totalTime), 160, 315, 42, 2, Color.GREEN); // BOMBTIME

	}

	private void presentFinish() {
		if (success) {
			graphics.drawUniversalText("Defused!", 2, true, 160, 66, 28, 255, Color.BLUE);
			if (gameType == 1) {
				graphics.drawSpecialText("+ " + (instanceScore), 160, 90, 16, 2, Color.BLUE);
			}
		} else {

			graphics.drawRect3(0, 31, 320, 450, alphaDeath, 255, 0, 0);
			graphics.drawHiddenText(bomb.nextWireToCut + 1 + "."
					+ bomb.colors[bomb.wireOrder[bomb.nextWireToCut]], 32 + ((bomb.nextWireToCut % 5) * 64),
					50 + ((bomb.nextWireToCut / 5) * 20), 255, 13, Color.DKGRAY);
			graphics.drawUniversalText("U R DEAD", 2, true, 160, 240, 46, 255, Color.BLACK);
		}
	}
}
